package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordReader {
	private static Logger logger = LogManager.getLogger(DynamicObjectHandler.class);
	private File wordfFile;
	private StringBuffer textToSearch;

	public WordReader(File wordfFile, StringBuffer textToSearch) {
		super();
		this.wordfFile = wordfFile;
		this.textToSearch = textToSearch;
	}

	public int search() {
		logger.info("WordReader - search - BEGIN");
		int returnValue = -1;
		String docText ="";
		// WordExtractor extractor = null;
		
		try {
			
			FileInputStream fis = new FileInputStream(wordfFile.getAbsolutePath());
			

			String fileExtension= wordfFile.getAbsolutePath().substring(
						wordfFile.getAbsolutePath().lastIndexOf(".")+1,wordfFile.getAbsolutePath().length());
			
			
			
			if(fileExtension.equals("docx")){
				 XWPFDocument docIn = new XWPFDocument(fis);
				 XWPFWordExtractor extractor= new XWPFWordExtractor(docIn);
				 
				 
				 docText= extractor.getText();
				 
				 

//				 logger.info("doc text: "+docText);
			}else{
				HWPFDocument document = new HWPFDocument(fis);
				WordExtractor extractor = new WordExtractor(document);
				docText = extractor.getText();
				
			}
			
			if(docText.contains(textToSearch.toString())){
				returnValue=docText.indexOf(textToSearch.toString());
			}
			
			
		} 
//		catch(OfficeXmlFileException officexmlFileException){
//			FileInputStream fis;
//			try {
//				fis = new FileInputStream(wordfFile.getAbsolutePath());
//				XWPFDocument docIn;
//				try {
//					docIn = new XWPFDocument(fis);
//					XWPFWordExtractor extractor= new XWPFWordExtractor(docIn);
//					docText= extractor.getText();
//					
//					if(docText.contains(textToSearch.toString())){
//						returnValue=docText.indexOf(textToSearch.toString());
//					}
//				} catch (IOException e2) {
//					logger.error(e2);
//				}
//				
//				
//				return returnValue;
//			} catch (FileNotFoundException e1) {
//				logger.error(e1);
//			}
//			
//			
//			
//			
//		} 
		catch (Exception e) {			
			
			logger.error("Errore", e);
			logger.info("WordReader - search - END");
			return returnValue;
		}
		logger.info("WordReader - search - END");
		return returnValue;

	}

	public static void main(String[] args) {
		File testFile = new File("c:/Users/Administrator/Desktop/cercaSuFiletester.docx");
		StringBuffer textToFind = new StringBuffer("ciccio");

		WordReader wr = new WordReader(testFile, textToFind);

		int searchTest=-1;
		
		searchTest = wr.search();
		
		
		logger.debug(searchTest);
		
	}

}
